
/**   
*    
* 项目名称：a_yongan   
* 类名称：BusinessServiceImpl   
* 类描述：   
* 创建人：liubo   
* 创建时间：2018年6月17日 上午10:51:24    
* 修改时间：2018年6月17日 上午10:51:24   
* 修改备注：   
* @version    
*    
*/
package com.yongan.business.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.yongan.business.dao.BusinessDao;
import com.yongan.business.entity.Business;
import com.yongan.business.service.BusinessService;
import com.yongan.user.enums.UserStatEnum;
import com.yongan.user.exception.UserException;
import com.yongan.util.Execution;

/**
 * @author liubo
 *
 */
@Service
public class BusinessServiceImpl implements BusinessService{

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private BusinessDao businessDao;
	private final String slat = "dsjdajsdkHJJHAJAKdsd921e0p12*DAe#@$@!qew";
	/* （非 Javadoc）
	 * @see com.yongan.Business.service.BusinessService#getBusinessList(int, int, java.lang.String)
	 */
	@Override
	public List<Business> getBusinessList(int offset, int limit, String type) {
		// TODO 自动生成的方法存根
		return businessDao.queryAll(offset, limit, type);
	}

	/* （非 Javadoc）
	 * @see com.yongan.Business.service.BusinessService#getBusinessById(java.lang.String)
	 */
	@Override
	public Business getBusinessById(String businessId) {
		// TODO 自动生成的方法存根
		return businessDao.queryById(businessId);
	}

	/* （非 Javadoc）
	 * @see com.yongan.Business.service.BusinessService#executeDept(com.yongan.Business.entity.Business, java.lang.String, java.lang.String)
	 */
	@Override
	public Execution executeBusiness(Business business, String businessId,
			String md5) throws UserException {
		System.out.println(businessId+"=========");
		if(md5 == null || !md5.equals(getMD5(businessId))){
			//throw new UserException("user data rewrite");
		}
		try{
			if(business == null){
				int deleteCount = businessDao.deleteBusiness(businessId);
				if(deleteCount<=0){
					throw new UserException("delete failed");
				}else{
					return new Execution(businessId, UserStatEnum.SUCCESS);
				}
			}else if((businessId.equals(""))){
				int addCount = businessDao.insertBusiness(business);
				if(addCount<=0){
					throw new UserException("add failed");
				}else{
					return new Execution(business.getBusinessId(), UserStatEnum.SUCCESS);
				}
			}else if(!(businessId.equals(""))){
				int updateCount = businessDao.updateBusiness(business);
				if(updateCount<=0){
					throw new UserException("update failed");
				}else{
					return new Execution(businessId, UserStatEnum.SUCCESS);
				}
			}else{
				throw new UserException("parameter error");
			}
			
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			throw new UserException("inner error:"+e.getMessage());
		}
	}

	/* （非 Javadoc）
	 * @see com.yongan.Business.service.BusinessService#getCountByType(java.lang.String)
	 */
	@Override
	public int getCountByType(String type) {
		// TODO 自动生成的方法存根
		return businessDao.queryCountByType(type);
	}
	private String getMD5(String Id){
		String base = Id + "/" + slat;
		String md5 = DigestUtils.md5DigestAsHex(base.getBytes());
		return md5;
	}

	
	@Override
	public List<Business> queryAllTree() {
		return businessDao.queryAllTree();
	}

}
